function [lpostd,flag_ok]= lmjMinCsminwelSub(parest, parvec, parposest, indmat, funcmod, Y, trainvec, prpar, prss, solveopt, addsol, ssposest, flag_transform)
% ======================================================================
% lmjMinCsminwelSub
%
% Called from estimaLoopOuter in the case of  single
% Called from lmjMinCsminwel.m in the case of parallel
%
% formely known as LMJ_POSTOPTMIN_CENTRAL.M
% 
% Computes the log Posterior for the various cases defined by likelCentral.

% [lpostd,flag_ok]= lmjMinCsminwel(parest, parvec, parposest,
% indmat, funcmod, Y, trainvec, prpar, prss, solveopt, addsol, ssposest, flag_transform)
%
% LPOSTD        == 1e20 if there are problems 
% FLAG_OK       ==0 if there are problems 
%               Different from FLAG_LIKELOK, from all likelihood
%               optimization routines, which assign -1e20 but not passed
%               here
%
% Follows the structure of GENSYSPOST_VEC by Alejandro Justiniano (C)
% IN addition to putting priors on parameters, our current model put
% priors on steady state values.
%
% LIKEL: Default value for problems is 1e40 
% =======================================================================
lpostd=1e20;flag_ok=0;
% 1. Transform to go from MIN to MOD
% ----------------------------------
if flag_transform ==1
    parest=feval(@min2mod,parest,indmat);
    %[parest,flag_c]=feval(@check_min,parest,indmat);
    %if flag_c==0;disp('Problems with transformed coefficients');return;end
end
%disp('Adding PAREST to PARVEC'); 
parvec(parposest)=parest; 

%% Taken from likelCentral 
%  Function handle is in addsol.funcLikel
[likel,ssvec,flag_likelok]=feval(addsol.funcLikel,parest,parvec,parposest,funcmod,Y,trainvec,solveopt,addsol);
if flag_likelok==0; 
    return
end
if isinf(abs(likel));
    disp('Likelihood is infinite!')
    return
end
%% Prior Density of Parameters
lpriord=sum(feval(@priordens,parvec, prpar.prior, prpar.alphap, prpar.betap, prpar.lbnd, prpar.ubnd));
if isinf(abs(lpriord))==1;return;end
%% Prior Density on Steady State
if ~isempty(ssposest)
    lpriord_ss = sum(feval(@priordens,ssvec(ssposest), prss.prior, prss.alphap, prss.betap, prss.lbnd, prss.ubnd));
else
    lpriord_ss=0;
end
if isinf(abs(lpriord_ss))==1;return;end
%% log likelihood
lpostd = -(lpriord + lpriord_ss + likel);
flag_ok=1; 